<extend name="Public/base"/>

<block name="body">
    <!-- 标题栏 -->
    <div class="main-title">
        <h2>[{$model['title']}] 列表</h2>
    </div>
	<div class="cf">
		<!-- <div class="fl">
		<empty name="model.extend">
			<div class="tools">
				<a class="btn btn-primary" href="{:U('add?model='.$model['id'])}">新 增</a>
				<button class="btn ajax-post confirm btn-primary" target-form="ids" url="{:U('del?model='.$model['id'])}">删 除</button>
			</div>
		</empty>
		</div> -->
		<!-- 高级搜索 -->
		<!-- <div class="search-form fr cf">
			<div class="sleft">
				<input type="text" name="{$model['search_key']|default='title'}" class="search-input" value="{:I('title')}" placeholder="请输入关键字">
				<a class="sch-btn" href="javascript:;" id="search" url="{:U('Think/lists','model='.$model['name'],false)}"><i class="btn-search"></i></a>
			</div>

		</div> -->
	</div>

    <!-- 兑换面板 -->
    <div class="redeem—window">
      <form action="{:U('redeemcodelists?model='.$model['name'])}" method="post" id="redeem-form">
        <div class="redeem-input">
            <input id="redeem-input" type="text" placeholder="输入手机号/兑换码" class="form-control" name="redeem-input">
            <div class="qrcode" onclick="scanOpen()"><i class="fa fa-qrcode"></i></div>
        </div>

        <div class="redeem-grid">
          <ul>
            <li>
                <div class="grid" onclick="inputNum('1')">1</div>
            </li>
            <li>
                <div class="grid" onclick="inputNum('2')">2</div>
            </li>
            <li>
                <div class="grid" onclick="inputNum('3')">3</div>
            </li>
            <li>
                <div class="grid" onclick="inputNum('4')">4</div>
            </li>
            <li>
                <div class="grid" onclick="inputNum('5')">5</div>
            </li>
            <li>
                <div class="grid" onclick="inputNum('6')">6</div>
            </li>
            <li>
                <div class="grid" onclick="inputNum('7')">7</div>
            </li>
            <li>
                <div class="grid" onclick="inputNum('8')">8</div>
            </li>
            <li>
                <div class="grid" onclick="inputNum('9')">9</div>
            </li>
            <li>
                <div class="grid" onclick="deleteNum()">删除</div>
            </li>
            <li>
                <div class="grid" onclick="inputNum('0')">0</div>
            </li>
            <li>
                <button type="button" class="grid" target-form="form-horizontal" id="redeemSubmit">确认</button>
            </li>
          </ul>
        </div>

        <div class="redeem-clear-filter">
            <button type="button" class="clearFilter" target-form="form-horizontal" onclick="clearFilter()">清除搜索记录并显示所有兑换码</button>
        </div>
      </form>
    </div>

    <!-- 数据列表 -->
    <div class="data-table redeem-list">
        <div class="data-table table-striped">
            <table>
                <!-- 表头 -->
                <thead>
                    <tr>
                        <!-- <th class="row-selected row-selected">
                            <input class="check-all" type="checkbox">
                        </th> -->
                        <volist name="list_grids" id="field">
                            <th>{$field.title}</th>
                        </volist>
                        <th>操作</th> 
                    </tr>
                </thead>

                <!-- 列表 -->
                <tbody>
                    <volist name="list_data" id="data">
                        <tr>
                            <!-- <td><input class="ids" type="checkbox" value="{$data['id']}" name="ids[]"></td> -->
                            <volist name="list_grids" id="grid">
                                <td>{:get_list_field($data,$grid)}</td>
                            </volist>
                            <td>
                              <if condition="$data.status eq '未兑换'">
                                <button type="button" class="btn btn-primary" onclick="exchangeRedeemcode({$data.id})">兑换</button>
                                <else />
                                <button type="button" class="btn btn-primary disabled">已兑换</button>
                              </if>
                            </td> 
                        </tr>
                    </volist>
                </tbody>
            </table>
        </div>
        <div class="page">
            {$_page|default=''}
        </div>
    </div>
    <div class="scancover">
        <div class="box">
            <i class="fa fa-close" onclick="scanClose()"></i>
            <h2>扫描兑换码/订单码</h2>
            <br />
            <div class="putin">
                <input type="text" place="兑换码" id="scaninput">
                <!-- <div class="inputcover"></div> -->
            </div>
        </div>
    </div>
</block>

<block name="script">
<script type="text/javascript">

// $(function(){
// 	//搜索功能
// 	$("#search").click(function(){
// 		var url = $(this).attr('url');
//         var query  = $('.search-form').find('input').serialize();
//         query = query.replace(/(&|^)(\w*?\d*?\-*?_*?)*?=?((?=&)|(?=$))/g,'');
//         query = query.replace(/^&/g,'');
//         if( url.indexOf('?')>0 ){
//             url += '&' + query;
//         }else{
//             url += '?' + query;
//         }
// 		window.location.href = url;
// 	});

//   //回车自动提交
//   $('.search-form').find('input').keyup(function(event){
//       if(event.keyCode===13){
//           $("#search").click();
//       }
//   });

$('#redeemSubmit').click(function(){
    var redeem_code = $('#redeem-input').val();
    window.location.href = 'index.php?s=Admin/Redeemcode/redeemcodelists&model=ticket_redeem_code&redeemInput='+redeem_code;
});

function clearFilter(){
    window.location.href = 'index.php?s=Admin/Redeemcode/redeemcodelists&model=ticket_redeem_code';
}

function inputNum(num){
  var redeem_str = $('#redeem-input').val();
  $('#redeem-input').val(redeem_str+num);
}

function deleteNum(){
  var redeem_str = $('#redeem-input').val();
  var len = redeem_str.length;
  $('#redeem-input').val(redeem_str.substr(0,len-1));
}

function exchangeRedeemcode(redeem_id){
  $.ajax({
    type : 'post',
    url : 'index.php?s=Admin/Redeemcode/exchangeRedeemcode',
    data : { id : redeem_id},
    success: function(data){
      console.log(data);
      window.location.reload();
    }
  })
}

function scanOpen(){
    $('.scancover').show();
    $('#scaninput').focus();
}

function scanClose(){
    // $('#scaninput').val('');
    // $('.tip').remove();
    // $('.second').remove();
    // $('.scancover').hide();
    history.go(0);
}

$('#scaninput').bind('input propertychange', function() {
    var ticketNum = $('#scaninput').val();
    if(ticketNum.length>=11){
        $.ajax({
            type : 'post',
            url : 'index.php?s=Admin/Redeemcode/exchangeRedeemcodeByTypes',
            data : { "redeem_num" : ticketNum},
            success: function(data){
              $('.tip').remove();
              $('.second').remove();
              if(data.status==0){
                $('#scaninput').val('');
                $('.scancover .box').append("<p class='tip'>"+data.info+"</p><p class='second'>请重新扫描</p>");
              }else if(data.status==1){
                //兑换码成功
                $('.scancover .box').append("<p class='tip'>兑换成功</p><div class='result'><p><b>购买者手机号：</b>"+data.info.visitor_phone+"</p><p><b>展览：</b>"+data.info.exhibition_name+"</p><p><b>票种：</b>"+data.info.ticket_name+"</p><button class='btn btn-primary' onclick='clearCode()' style='margin: 10px auto;display: block;width: 100px;'>继续</button></div>");
              }else{
                //确认是否核销订单
                $('.scancover .box').append("<p class='tip'>是否确认兑换该订单下所有票券？</p><div class='result'><p><b>展览：</b>"+data.info.exhibition_name+"</p><p><b>票种：</b>"+data.info.ticket_name+"</p><p><b>张数：</b>"+data.info.amount+"</p><div style='text-align:center'><button class='btn btn-primary' onclick='exchangeRedeemcodeByOrder("+data.info.id+")' style='margin: 10px;display: inline-block;width: 100px;'>确认兑换</button><button class='btn btn-primary' onclick='clearCode()' style='margin: 10px;display: inline-block;width: 100px;'>取消兑换</button></div></div>");
              }
            }
        })
    }
    
});

function clearCode(){
    $('#scaninput').val('');
    $('.tip').remove();
    $('.result').remove();
    $('#scaninput').focus();
}

function exchangeRedeemcodeByOrder(ticket_order_id){
    $.ajax({
        type : 'post',
        url : 'index.php?s=Admin/Redeemcode/exchangeRedeemcodeByOrder',
        data : { "ticket_order_id" : ticket_order_id},
        success: function(data){
          $('.tip').remove();
          $('.second').remove();
          $('.result').remove();
          if(data.status==0){
            $('#scaninput').val('');
            $('.scancover .box').append("<p class='tip'>"+data.info+"</p><p class='second'>请重新扫描</p>");
          }else{
            //兑换码成功
            $('.scancover .box').append("<p class='tip'>兑换成功</p><div class='result'><p><b>购买者手机号：</b>"+data.info.visitor_phone+"</p><p><b>展览：</b>"+data.info.exhibition_name+"</p><p><b>票种：</b>"+data.info.ticket_name+"</p><p><b>张数：</b>"+data.info.amount+"</p><button class='btn btn-primary' onclick='clearCode()' style='margin: 10px auto;display: block;width: 100px;'>继续</button></div>");
          }
        }
    })
}
</script>
</block>
